package cn.zhl.leetcode.链表;

/**
 * @program: demos
 * @description:
 * @author: 刘振华
 * @create: 2020-04-03 10:25
 * https://leetcode-cn.com/problems/linked-list-cycle/
 **/
public class _141_环形链表 {
	//    利用快慢指针思路来解决问题，慢指针一次走一步，快指针一次走两步，如果相遇，证明有环
	public boolean hasCycle(ListNode head) {
		if (head == null || head.next == null) {
			return false;
		}
		ListNode slow = head;
		ListNode fast = head.next;
		while (fast != null && fast.next != null) {
			if (slow == fast) {
				return true;
			}
			slow = slow.next;
			fast = fast.next.next;
		}
		return false;
	}
}
